<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="是否可见" prop="visible">
        <el-select
          v-model="queryParams.visible"
          placeholder="请选择是否可见"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="评论信息" align="center" prop="user" min-width="100px">
        <template #default="scope">
          <el-avatar
            fit="cover"
            :src="scope.row.user?.avatar"
            class="flex-none w-50px h-50px"
            @click="imagePreview(scope.row.user?.avatar)"
          />
          <br />
          <el-text>{{ scope.row.user?.nickname || '匿名' }}</el-text>
          <br />
          <br />
          <el-link v-if="scope.row.toUser" type="primary">
            @{{ scope.row.toUser.nickname || '匿名' }}&nbsp;
          </el-link>
          <el-text>{{ scope.row.content }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="视频信息" align="center" prop="videoInfo" width="220px">
        <template #default="scope">
          <VideoPlayer
            :width="200"
            :title="scope.row.vlogTitle"
            :videoUrl="scope.row.vlogPlayUrl"
            :posterUrl="scope.row.vlogCoverUrl"
            :autoplay="false"
          />
        </template>
      </el-table-column>
      <el-table-column label="其他信息" align="center" prop="visible" min-width="100px">
        <template #default="scope">
          是否可见：<dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.visible" />
          <br />
          <el-text>创建时间：{{ formatDate(scope.row.createTime) }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)">
            修改可见性
          </el-button>
          <br />
          <el-button
            link
            type="primary"
            @click="openLikedList(scope.row.id)"
            v-hasPermi="['vlog:comment:query']"
          >
            查看点赞用户
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 设置可见性弹窗 -->
  <CommentVisibleUpdateForm ref="formRef" />
  <!-- 点赞用户列表弹窗 -->
  <CommentLikedList ref="likedListRef" />
</template>

<script setup lang="ts">
import { VLogCommentApi, VLogCommentVO } from '@/api/vlog/comment'
import { createImageViewer } from '@/components/ImageViewer'
import { DICT_TYPE, getBoolDictOptions } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import CommentLikedList from './components/CommentLikedList.vue'
import CommentVisibleUpdateForm from './components/CommentVisibleUpdateForm.vue'

/** 视频评论 列表 */
defineOptions({ name: 'VLogComment' })

const loading = ref(true) // 列表的加载中
const list = ref<VLogCommentVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  vlogId: undefined,
  createTime: undefined,
  visible: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await VLogCommentApi.getVLogCommentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 大图预览 */
const imagePreview = (imgUrl: string) => {
  createImageViewer({
    urlList: [imgUrl]
  })
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 设置可见性操作 */
const formRef = ref()
const openForm = (type: string, id?: number | string) => {
  formRef.value.open(type, id)
}

/** 点赞的用户列表 */
const likedListRef = ref()
const openLikedList = (id: number | string) => {
  likedListRef.value.open(id)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
